package cn.buk.tms.eotms.entity;

import cn.buk.common.util.DateUtil;
import com.fasterxml.jackson.annotation.JsonFormat;

import jakarta.persistence.*;
import java.util.Date;

/**
 * Created by yfdai on 2017/7/16.
 */
@NamedQuery(
        name = "BillTrainTicket.searchMonthly",
        query = "select new cn.buk.tms.eotms.report.dto.ReportTrainDto(" +
                "o.trainNo, o.dcity, o.acity, o.ddate, o.seatClass, o.price + o.serviceCharge," +
                "b.opDate, b.op1, b.id, b.bePaid) " +
                "from BillTrainTicket o left join BillInfo b on (o.billNum = b.billNum and o.enterpriseId = b.enterpriseId) " +
                " where o.enterpriseId = :enterpriseId and o.customerId = :customerId and b.opDate >= :beginDate and b.opDate <= :endDate " +
                " order by b.opDate, o.ddate"
)
@Entity
@Table(name = "train_ticket")
public class BillTrainTicket {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private int id;

    @Column(name = "enterprise_id")
    private int enterpriseId;

    /**
     * 为该客户预定的火车票
     */
    @ManyToOne
    @JoinColumn(name="customer_id")
    private Customer customer;

    @Column(name = "customer_id", insertable = false, updatable = false)
    private Integer customerId;

    /**
     * 账单号
     */
    @Column(name = "bill_num")
    private String billNum;

    /**
     * 销售日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd",timezone="GMT+8")
    @Temporal(TemporalType.DATE)
    @Column(name="sale_date")
    private Date saleDate;

    /**
     * 付款方式
     */
    @Column(length = 2)
    private String payMode = "8";

    /**
     * 姓名
     */
    @Column(name="psg_name", length=50)
    private String passengerName;

    @Column(name = "psg_count")
    private Integer psgCount;


    /**
     * 车次
     */
    @Column(name="train_no", length=50)
    private String trainNo;

    /**
     * 出发地
     */
    @Column(length=50)
    private String dcity;

    /**
     * 目的地
     */
    @Column(length=50)
    private String acity;

    /**
     * 日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd",timezone="GMT+8")
    @Temporal(TemporalType.DATE)
    private Date ddate;

    /**
     * 座位等级
     */
    @Column(name = "seat_class", length = 50)
    private String seatClass;

    /**
     * 价格/人
     */
    private double price;

    /**
     * 服务费(总)
     */
    @Column(name = "service_charge")
    private double serviceCharge;

    private String remark;

    /**
     * 是否已付款
     */
    @Column(name = "be_paid", length = 1)
    private String bePaid;


    /**
     * 保存数据库的时间：系统自动生成
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="create_time", insertable = false, updatable = false)
    private Date createTime;


    public String getTrainDetail() {
        return this.trainNo + " " + DateUtil.formatDate(this.ddate, "yyyy-MM-dd") + " " + this.dcity + " " + this.acity + " " + this.seatClass;
    }

    public double getTotal() {
        return this.price * this.getPsgCount() + this.serviceCharge;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getEnterpriseId() {
        return enterpriseId;
    }

    public void setEnterpriseId(int enterpriseId) {
        this.enterpriseId = enterpriseId;
    }

    public Customer getCustomer() {
        return this.customer == null ? new Customer() : this.customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public String getBillNum() {
        return billNum;
    }

    public void setBillNum(String billNum) {
        this.billNum = billNum;
    }

    public Date getSaleDate() {
        return saleDate;
    }

    public void setSaleDate(Date saleDate) {
        this.saleDate = saleDate;
    }

    public String getPayMode() {
        return payMode;
    }

    public void setPayMode(String payMode) {
        this.payMode = payMode;
    }

    public String getPassengerName() {
        return passengerName;
    }

    public void setPassengerName(String passengerName) {
        this.passengerName = passengerName;
    }

    public String getTrainNo() {
        return trainNo;
    }

    public void setTrainNo(String trainNo) {
        this.trainNo = trainNo;
    }

    public String getDcity() {
        return dcity;
    }

    public void setDcity(String dcity) {
        this.dcity = dcity;
    }

    public String getAcity() {
        return acity;
    }

    public void setAcity(String acity) {
        this.acity = acity;
    }

    public Date getDdate() {
        return ddate;
    }

    public void setDdate(Date ddate) {
        this.ddate = ddate;
    }

    public String getSeatClass() {
        return seatClass;
    }

    public void setSeatClass(String seatClass) {
        this.seatClass = seatClass;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getServiceCharge() {
        return serviceCharge;
    }

    public void setServiceCharge(double serviceCharge) {
        this.serviceCharge = serviceCharge;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getBePaid() {
        return bePaid;
    }

    public void setBePaid(String bePaid) {
        this.bePaid = bePaid;
    }

    public Integer getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Integer customerId) {
        this.customerId = customerId;
    }

    public int getPsgCount() {
        return psgCount == null ? 1 : psgCount;
    }

    public void setPsgCount(Integer psgCount) {
        this.psgCount = psgCount;
    }

}
